//
//  MAPYConnectResultViewController.swift
//  Mars Proxy
//
//  Created by PowerfullClean on 2023/10/16.
//

import UIKit

class MAPYConnectResultViewController: MAPYResultViewController {
    init(mapy_connected: Bool = true) {
        super.init(nibName: nil, bundle: nil)
        self.mapy_connected = mapy_connected
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !mapy_connected {
            mapy_navView.mapy_titleLabel.text = "DISCONNECTED"
        }
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(mapy_enterActive),
            name: UIApplication.didBecomeActiveNotification, object: nil)
    }
    // MARK: - mapy action
    override func mapy_layout() -> UICollectionViewFlowLayout {
        let mapy_layout = UICollectionViewFlowLayout()
        mapy_layout.minimumLineSpacing = 0
        mapy_layout.minimumInteritemSpacing = 0
        return mapy_layout
    }
    override func mapy_collectionViews() {
        super.mapy_collectionViews()
        view.addSubview(mapy_navView)
        mapy_collectionView.snp.makeConstraints { make in
            make.bottom.left.width.equalToSuperview()
            make.top.equalTo(mapy_navView.snp.bottom)
        }
        mapy_collectionView.register(MAPYConnectResultCell.classForCoder(), forCellWithReuseIdentifier: MAPYConnectResultCell.mapy_viewToString)
        mapy_collectionView.register(MAPYConnectResultItemCell.classForCoder(), forCellWithReuseIdentifier: MAPYConnectResultItemCell.mapy_viewToString)
        mapy_collectionView.dataSource = self
        mapy_collectionView.delegate = self
        mapy_collectionView.contentInset = UIEdgeInsets(top: 0, left: 15, bottom: MAPYCommon.mapy_XBottomSpace+15, right: 15)
    }
    @objc func mapy_enterActive() {
        if mapy_connected && (MAPYManager.mapy_manager.mapy_vpnCurStateType == .mapy_invalid || MAPYManager.mapy_manager.mapy_vpnCurStateType == .mapy_disconnected) {
            mapy_navView.mapy_titleLabel.text = "DISCONNECTED"
            mapy_connected = false
            mapy_collectionView.reloadData()
        }
    }
    // MARK: - mapy lazy
    var mapy_connected = true
    override func mapy_baseBgName() -> String? {
        return "mapy_list_bg"
    }
    lazy var mapy_navView: MAPYServersListNavView = {
        let mapy_navView = MAPYServersListNavView()
        mapy_navView.mapy_titleLabel.text = "CONNECTED"
        mapy_navView.mapy_leftBlock = {[weak self] in
            self?.navigationController?.popToRootViewController(animated: true)
        }
        return mapy_navView
    }()
    lazy var mapy_funcModels: [MAPYFuncModel] = {
        var mapy_funcModels = [MAPYFuncModel]()
        mapy_funcModels.append(MAPYFuncModel(mapy_type: .mapy_speed,mapy_title: "Speed test", mapy_icon: "mapy_result_connectSpeed"))
        mapy_funcModels.append(MAPYFuncModel(mapy_type: .mapy_ip,mapy_title: "IP information", mapy_icon: "mapy_result_connectIP"))
        return mapy_funcModels
    }()
    
}
extension MAPYConnectResultViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 2
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if section == 1 {
            return mapy_funcModels.count
        }
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if indexPath.section == 1 {
            let mapy_cell = collectionView.dequeueReusableCell(withReuseIdentifier: MAPYConnectResultItemCell.mapy_viewToString, for: indexPath) as! MAPYConnectResultItemCell
            mapy_cell.mapy_funcModel = mapy_funcModels[indexPath.row]
            return mapy_cell
        }
        let mapy_cell = collectionView.dequeueReusableCell(withReuseIdentifier: MAPYConnectResultCell.mapy_viewToString, for: indexPath) as! MAPYConnectResultCell
        if !mapy_connected {
            mapy_cell.mapy_successLabel.text = "Successfully disconnected to VPN\nwelcome to VPN again"
            mapy_cell.mapy_iconView.image = UIImage(named: "mapy_result_disConnectIcon")
            mapy_cell.mapy_timeLabel.text = "-:-:-"
            mapy_cell.mapy_titleLabel.text = ""
            mapy_cell.mapy_stopConnectTimer()
        }else {
            mapy_cell.mapy_serverModel = MAPYManager.mapy_manager.mapy_curServerModel
        }
        return mapy_cell
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if indexPath.section == 1 {
            return CGSize(width: floor((mapy_SCREEN_WIDTH-30)/2.0), height: 173)
        }
        return CGSize(width: mapy_SCREEN_WIDTH-30, height: 243+8+68)
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.section == 0 {
            return
        }
        if MAPYManager.mapy_manager.mapy_netType == .mapy_none {
            MAPYHudCommon.mapy_showMessage("No available network", isError: false)
            return
        }
        let mapy_funcModel = mapy_funcModels[indexPath.row]
        navigationController?.pushViewController(MAPYScanViewController(mapy_funcType: mapy_funcModel.mapy_type), animated: true)
    }
}

